package com.zjeav.lingjiao.ui.book;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.maning.mndialoglibrary.MProgressDialog;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.BaseActivity;
import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.baseBean.Book;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.AuthorizeCard;
import com.zjeav.lingjiao.base.request.BookAuthorizeRequest;
import com.zjeav.lingjiao.base.request.ValidateCode;
import com.zjeav.lingjiao.base.tools.SystemTools;
import com.zjeav.lingjiao.base.utils.ErrorUtils;
import com.zjeav.lingjiao.ui.presenter.BookPresenter;


public class SaoMiaoBookActivity extends BaseActivity implements View.OnClickListener, SaomiaoBookContract.View {
    private ImageView book_cover_img;
    private TextView book_subject_txt, book_info_txt, sure_txt;
    BookPresenter bookPresenter;
    String content;
    AuthorizeCard card;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saomiaobook_layout);
        init();

    }

    private void init() {
        content = getIntent().getStringExtra("content");
        book_cover_img = findViewById(R.id.book_cover_img);
        book_subject_txt = findViewById(R.id.book_subject_txt);
        book_info_txt = findViewById(R.id.book_info_txt);
        sure_txt = findViewById(R.id.sure_txt);
//        back_txt = findViewById(R.id.back_txt);
        bookPresenter = new BookPresenter(this);
        sure_txt.setOnClickListener(this);
//        back_txt.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        card = JSONObject.parseObject(content, AuthorizeCard.class);
        bookPresenter.getSaoMiaoResult(card);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sure_txt:
                MProgressDialog.showProgress(this, "添加课本...");
                BookAuthorizeRequest request = new BookAuthorizeRequest(card.getBookid(), card.getAuthorizeCardId(), SystemTools.getSystemModel(), SystemTools.getUniqueId(getApplicationContext()));
                bookPresenter.addBook(request);
                break;
//            case R.id.back_txt:
//                break;
        }
    }

    @Override
    public void showSaomiaoResult(Book book) {
        if (book != null) {
            if (book.getCover() != null) {
                Glide.with(this)
                        .load(book.getCover())
                        .into(this.book_cover_img);
            }
            this.book_subject_txt.setText(book.getSubject());
//            String result = String.format(getResources().getString(R.string.saomiaoresult), book.getTitle(), content, book.getIntro());
            this.book_info_txt.setText(book.getIntro());
        } else {

        }

    }

    @Override
    public void addBook(Result result) {
        MProgressDialog.dismissProgress();
        Toast.makeText(this, "添加课本成功!", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void addFail(Throwable throwable, String message) {
        MProgressDialog.dismissProgress();
        Toast.makeText(this, ErrorUtils.showError(throwable, message), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(Throwable throwable, String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        finish();
    }
}
